package de.tabreaderandroid.tabreader.activities;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import de.tabreaderandroid.tabreader.R;
import de.tabreaderandroid.tabreader.preferences.FolderRepository;

/**
 * Activity for managing the folders the tabs are read from.
 * 
 * @author André Stannek
 * 
 */
public class SelectFoldersActivity extends Activity {

	private static final int SELECT_FOLDER_REQUEST = 1;
	private static final int REMOVE_DIALOG = 0;
	private FolderRepository folderRepository;

	/**
	 * 
	 */
	public SelectFoldersActivity() {
		super();
		folderRepository = new FolderRepository(this);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_folders);
		ListView list = (ListView) findViewById(R.id.folder_list);
		list.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1));
		list.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Bundle args = new Bundle();
				args.putString("folder",
						(String) parent.getItemAtPosition(position));
				showDialog(REMOVE_DIALOG, args);
			}
		});
		loadFolders();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.select_folders, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_add_folder:
			Intent intent = new Intent();
			intent.setAction(Intent.ACTION_PICK);

			intent.setData(Uri.parse("folder://"));
			intent.putExtra(Intent.EXTRA_TITLE, "Select Folder");
			startActivityForResult(
					Intent.createChooser(intent, "Select Folder"),
					SELECT_FOLDER_REQUEST);
			break;
		default:
			return false;
		}
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			if (requestCode == SELECT_FOLDER_REQUEST) {
				folderRepository.addFolder(data.getData()
						.getSchemeSpecificPart().replaceFirst("//", ""));
				loadFolders();
			}
		}
	}

	@SuppressWarnings("unchecked")
	private void loadFolders() {
		List<String> folders = folderRepository.getAllFolders();
		ListView list = (ListView) findViewById(R.id.folder_list);
		ArrayAdapter<String> adapter = (ArrayAdapter<String>) list.getAdapter();
		adapter.clear();
		adapter.addAll(folders);
	}

	@Override
	public Dialog onCreateDialog(int id, final Bundle args) {
		switch (id) {
		case REMOVE_DIALOG:

			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Removing folder!");
			builder.setMessage("Do you want to remove the folder from the list?");
			builder.setPositiveButton("Yes", new OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					folderRepository.deleteFolder(args.getString("folder"));
					loadFolders();
				}
			});
			builder.setNegativeButton("No", new OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			return builder.create();
		default:
			return null;
		}

	}
}
